package priv.cwr.springboot.demo.mapper;

import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * User: R
 * Date: 2018/8/30
 * Time: 9:02
 * Created with IntelliJ IDEA.
 */
public class UserProvider {
    private String selectUserForm() {
        return "user_id," +
                "dept_id," +
                "name," +
                "phone";
    }

    /**
     * 查询员工列表(PC端)
     *
     * @param map
     * @return
     */
    public String findAll(Map<String, Object> map) {
        //员工名称
        String name = (String) map.get("name");
        //电话
        String phone = (String) map.get("phone");
        //部门
        String depts = (String) map.get("depts");
        return new SQL() {{
            SELECT(selectUserForm());
            FROM("t_user");
            if (name != null) {
                WHERE("name LIKE #{name}");
            }
            if (phone != null) {
                WHERE("phone = #{phone}");
            }
            if (depts != null) {
                WHERE("dept_id IN ( " + depts + " )");
            }
        }}.toString();
    }

    /**
     * 查询员工列表(移动端)
     *
     * @param map
     * @return
     */
    public String findAllByApp(Map<String, Object> map) {
        Long deptId = (Long) map.get("deptId");
        return new SQL() {{
            SELECT(selectUserForm());
            FROM("t_user");
            if (deptId != null) {
                WHERE("dept_id = #{deptId}");
            }
        }}.toString();
    }
}
